package com.fy.tt.utils;


import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

import static com.fy.tt.utils.BandCore.getAccessToken;


public class HttpClientUtil {

    public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            // 创建远程url连接对象
            URL url = new URL(httpurl);
            // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式：get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间：15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间：60000毫秒
            connection.setReadTimeout(60000);
            // 发送请求
            connection.connect();
            // 通过connection连接，获取输入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封装输入流is，并指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();// 关闭远程连接
        }

        return result;
    }


    public static String doPost(String httpUrl, String param) {

        HttpURLConnection connection = null;
        InputStream is = null;
        OutputStream os = null;
        BufferedReader br = null;
        String result = null;
        try {
            URL url = new URL(httpUrl);
            // 通过远程url连接对象打开连接
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接请求方式
            connection.setRequestMethod("POST");
            // 设置连接主机服务器超时时间：15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取主机服务器返回数据超时时间：60000毫秒
            connection.setReadTimeout(60000);

            // 默认值为：false，当向远程服务器传送数据/写数据时，需要设置为true
            connection.setDoOutput(true);
            // 默认值为：true，当前向远程服务读取数据时，设置为true，该参数可有可无
            connection.setDoInput(true);
            // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 设置鉴权信息：Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
            connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            // 通过连接对象获取一个输出流
            os = connection.getOutputStream();
            // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
            if(param!=null){
                os.write(param.getBytes());
            }
            // 通过连接对象获取一个输入流，向远程读取
            if (connection.getResponseCode() == 200) {

                is = connection.getInputStream();
                Map headers = connection.getHeaderFields();
                Set<String> keys = headers.keySet();
                 for( String key : keys ){
                     String val = connection.getHeaderField(key);
                     System.out.println(key+"    "+val);
                 }

                // 对输入流对象进行包装:charset根据工作项目组的要求来设置
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

                StringBuffer sbf = new StringBuffer();
                String temp = null;
                // 循环遍历一行一行读取数据
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 断开与远程地址url的连接
            connection.disconnect();
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        String url="https://www.wetoband.com/core//core/v4/authc/user?aid=21419389378723787447&format=json&gid=1";
        String param="name=qq1020224260&password=qq1020224260";
        String userName="qq1020224260";
        String passWord="qq1020224260";
        String accessToken=null;
        try{
             accessToken=getAccessToken(userName,passWord);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("accessToken:"+accessToken);
        String faceUrl="https://www.wetoband.com/tre/runTool4ward?" +
                "gid=5309707&tp=deb23d4e93e9e0e56e00a5c94ab3c935ee7ed76dac7b0b8e7aca979c936c0dc09cb125a9a95fcaf245edab6d05110733113b024c4cea693b2c04e7ef7b171487bbc9734486579554d78c54e9d356862ce2960b1e9f32a6109a646c547a80a8446345434c32575ca89d341aecce4c601fa88794b196c66e53211f9d681ab38eefa6166d0112df38f76bf87d6e63778efe45575167fd7d7d51908ebae5a7c2b9967b9a0064eecb549c9e4032e179569a3103438cb1627f5431daf0f62f9f0ba383cebd8ed1a03b4552126ea9f1e444a5f4ab28e2e52514a5a8f587ccc1599f03d2d706c00276e929f8647f20b348b5b0d37aca08824ee35fd97c1ef2ae01b4254f5ccb203d4078dc0b39e1fbd056562eb4939dc432add740bb5ac1788a63e9c589681e0289b6f6ad1e6e715bc7d7727cb002a288faf5da8828de1cb6140dde396b95e0862ce930dff369e6d2a265a1f60b34fd8d0ef4ea05ddc908d9db58c4be5b26be59ee788a476cac3d631ed8544dabbba8ac0f2d9891d39d3a1f366bd08bf6f3f1ea752322f7050fc1137e97cd016811a2ca96ef434d34882a9735d35469cbc2192089ab297c1aae86209d0df97f450f9b55db527cc36157fd686a0c0ef2e36bd8c4a0ea5dfdb022932770445de0c6e1b02e03714b167969c4559cf8c4468c416e689c4b5a606951433137971d864bc05dc774f8f94fd6ef35ce153313dc4c5f89b6ba964309007d6c06489e35b04018a769d73af72ce1459ffb0350d4658a0af665d7ad09729f604325aa7cf39761ca2fa1751c0f1734a05d4ba94cb9f076381d656eb9f0e4db2ff40d5eeaa46f2ac940edabb002b462&" +
                "url_route=get_image_from_history&device_id=12";
//        String faceRes=doGetWithAccessToken(faceUrl,accessToken);
//        String res=doPost(url,param);
//        System.out.println("faceRes:"+faceRes );
        try{
            Method doGetMethod=BandCore.class.getDeclaredMethod("doGetWithAccessToken",String.class,String.class);
            String faceRes=(String)doGetMethod.invoke(BandCore.class.newInstance(),faceUrl,accessToken);
            System.out.println("faceRes:"+faceRes );
            JSONObject jsonObject = JSONObject.parseObject(faceRes);

            System.out.println(jsonObject.toJSONString());
            System.out.println(jsonObject.getString("tips"));
            System.out.println(jsonObject.getString("unknown"));
            JSONObject unknow=jsonObject.getObject("unknow",jsonObject.getClass());
            System.out.println("unknow:"+unknow);
        }catch (Exception e){
            e.printStackTrace();
        }
//        Class clazz = Class.forName("com.language.utils.BandCore");
//        Method[] methods = clazz.getMethods();
//
//        methods = clazz.getDeclaredMethods();
//        for (Method m : methods) {
//            System.out.println(m);
//        }





    }

}
